package com.midou.administration.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.midou.administration.bo.ClerkBo;
import com.midou.administration.bo.ClerkSelectBo;
import com.midou.administration.entity.Clerk;
import com.midou.administration.result.Result;
import com.midou.administration.utils.TokenUtils;
import com.midou.administration.service.impl.ClerkServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/administration/clerk")
public class ClerkController {
    @Autowired
    private ClerkServiceImpl clerkService;
    @RequestMapping("/login")
    public Result login(@RequestBody Clerk clerk){
        System.out.println(clerk.getClerkAccountNumber()+"\t"+clerk.getClerkPassword());
        try{
            Clerk clerk1 = clerkService.login(clerk);
            String token = TokenUtils.getToken(clerk1.getClerkId(),clerk1.getClerkBusinessId());
            return new Result(200,"true",clerk1,token);
        }catch (Exception e){
            return new Result(404,e.getMessage(),null,null);
        }
    }
/*  *//** 员工登录 *//*
  @RequestMapping("/login")
  public Clerk doLogin(String name, String pwd){
    Clerk clerk = this.clerkService.doLogin(name,pwd);
    System.out.println(clerk.getClerkPhone());
    return clerk;
  }*/
    /**
     * 添加新员工
     */
    @RequestMapping("/add")
    public int createClerk(@RequestBody Clerk clerk){
        List<ClerkSelectBo> list=clerkService.chaCf(clerk);
            if (list.size()>0){
                return 2;
            }else {
                int c =clerkService.add(clerk);
                return c;
            }
    }
    /**
     * 查找所有员工信息
     */
    @RequestMapping("/selectall")
    public Page all(@RequestBody ClerkBo clerkBo){
        Page page=clerkService.all(clerkBo);

        return clerkService.all(clerkBo);
    }
    /**
     *点击编辑员工信息
     */
    @RequestMapping("/selectId")
    public List<ClerkSelectBo> slectId(@RequestBody Integer id){
        List<ClerkSelectBo> list=clerkService.slectId(id);
        return list;
    }

    @RequestMapping("/updateClerk")
    public int upda(@RequestBody ClerkSelectBo clerkSelectBo){
        int c=clerkService.upda(clerkSelectBo);
        return c;
    }
    /**
     * 删除员工
     */
    @RequestMapping("/deleteClerk")
    public int delete(@RequestBody Integer id){
        int c=clerkService.delteClerk(id);
        if (c>0){
            return c;
        }else {
            return 0;
        }
    }

    /**
     * 修改登陆密码
     */
    @RequestMapping("/queryupwd")
    public int queryupwd(@RequestBody ClerkSelectBo clerkSelectBo){
        int c=clerkService.quertupwd(clerkSelectBo);
        if (c>0){
            return 1;
        }else {
            return 0;
        }
    }



}
